package cn.cloudplug.friendsrecommend.view;

import cn.cloudplug.friendsrecommend.util.ScreenUtils;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

public class SlidingMenu extends HorizontalScrollView {

	// What fraction (1/x) of the screen the user must swipe to indicate a page
	// change
	/*
	 * Velocity of a swipe (in density-independent pixels per second) to force a
	 * swipe to the next/previous screen. Adjusted into
	 * mDensityAdjustedSnapVelocity on init.
	 */
	// Argument to getVelocity for units to give pixels per second (1 = pixels
	// per millisecond).

	private static final int TOUCH_STATE_REST = 0;
	private static final int TOUCH_STATE_HORIZONTAL_SCROLLING = 1;
	private static final int TOUCH_STATE_VERTICAL_SCROLLING = -1;
	private float mLastMotionX;
	private float mLastMotionY;
	// private OnScreenSwitchListener mOnScreenSwitchListener;
	private int mTouchSlop;
	private int mTouchState = TOUCH_STATE_REST;

	/**
	 * 屏幕宽度
	 */
	private int mScreenWidth;
	/**
	 * dp
	 */
	private int mMenuRightPadding;
	private int mMenuLeftPadding;
	/**
	 * 菜单的宽
	 */
	private int leftMenuWidth;
	private int rightMenuWidth;
	private int mHalfMenuWidth;

	private boolean isLeftOpen;
	private boolean isRightOpen;

	private boolean once;

	private ViewGroup mLeftMenu;
	private ViewGroup mRightMenu;
	private ViewGroup mContent;

	public SlidingMenu(Context context, AttributeSet attrs) {
		this(context, attrs, 0);

	}

	public SlidingMenu(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mScreenWidth = ScreenUtils.getScreenWidth(context);
		mMenuRightPadding = (int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_DIP, 100f, getResources()
						.getDisplayMetrics());
		mMenuLeftPadding = (int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_DIP, 100f, getResources()
						.getDisplayMetrics());
	}

	public SlidingMenu(Context context) {
		this(context, null, 0);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		/**
		 * 显示的设置一个宽
		 */
		if (!once) {
			LinearLayout wrapper = (LinearLayout) getChildAt(0);
			mLeftMenu = (ViewGroup) wrapper.getChildAt(0);
			mContent = (ViewGroup) wrapper.getChildAt(1);
			mRightMenu = (ViewGroup) wrapper.getChildAt(2);

			leftMenuWidth = mScreenWidth - mMenuRightPadding;
			rightMenuWidth = mScreenWidth - mMenuLeftPadding;
			mHalfMenuWidth = leftMenuWidth / 2;
			mLeftMenu.getLayoutParams().width = leftMenuWidth;
			mRightMenu.getLayoutParams().width = rightMenuWidth;
			mContent.getLayoutParams().width = mScreenWidth;

		}
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);

	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
		if (changed) {
			this.scrollTo(leftMenuWidth, 0);
			once = true;
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent ev) {

		int action = ev.getAction();
		switch (action) {
		// Up时，进行判断，如果显示区域大于菜单宽度一半则完全显示
		case MotionEvent.ACTION_UP:

			int scrollX = getScrollX(); // 显示右边菜单
			if (scrollX < mHalfMenuWidth) {
				this.smoothScrollTo(0, 0);
				isLeftOpen = true;
				isRightOpen = false;
			}
			if (scrollX > mHalfMenuWidth&& scrollX < mScreenWidth + leftMenuWidth - mHalfMenuWidth) {
				this.smoothScrollTo(leftMenuWidth, 0);
				isLeftOpen = false;
				isRightOpen = false;
			}
			if (scrollX > mScreenWidth + leftMenuWidth - mHalfMenuWidth) {
				this.smoothScrollTo(leftMenuWidth + rightMenuWidth, 0);
				isLeftOpen = false;
				isRightOpen = true;
			}
			return true;

		}

		return super.onTouchEvent(ev);
	}

	

	public void openLeftMenu() {
		if (isLeftOpen)
			return;
		this.smoothScrollTo(0, 0);
		isLeftOpen = true;
		isRightOpen = false;
	}

	public void openRightMenu() {
		if (isRightOpen)
			return;
		this.smoothScrollTo(leftMenuWidth + rightMenuWidth, 0);
		isLeftOpen = false;
		isRightOpen = true;
	}

	public void closeLeftMenu() {
		if (isLeftOpen) {
			this.smoothScrollTo(leftMenuWidth, 0);
			isLeftOpen = false;
			isRightOpen = false;
		}

	}

	public void closeRightMenu() {
		if (isRightOpen) {
			this.smoothScrollTo(leftMenuWidth, 0);
			isLeftOpen = false;
			isRightOpen = false;
		}

	}

	public void toggleZuo() {
		if (isLeftOpen) {
			closeLeftMenu();
		} else {
			openLeftMenu();
		}

	}

	public void toggleYou() {
		if (isRightOpen) {
			closeRightMenu();
		} else {
			openRightMenu();
		}

	}

	/*
	 * @Override protected void onScrollChanged(int l, int t, int oldl, int
	 * oldt) { super.onScrollChanged(l, t, oldl, oldt); float scale = l * 1.0f /
	 * leftMenuWidth; float leftScale = 1 - 0.3f * scale; float rightScale =
	 * 0.8f + scale * 0.2f;
	 * 
	 * ViewHelper.setScaleX(mLeftMenu, leftScale);
	 * ViewHelper.setScaleY(mLeftMenu, leftScale);
	 * ViewHelper.setAlpha(mLeftMenu, 0.6f + 0.4f * (1 - scale));
	 * ViewHelper.setTranslationX(mLeftMenu, leftMenuWidth * scale * 0.7f);
	 * 
	 * ViewHelper.setPivotX(mContent, 0); ViewHelper.setPivotY(mContent,
	 * mContent.getHeight() / 2); ViewHelper.setScaleX(mContent, rightScale);
	 * ViewHelper.setScaleY(mContent, rightScale);
	 * 
	 * }
	 */
}
